package service_k;

import mapper_k.HomeUserMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import pojo_k.HomeUser;
import pojo_k.HomeUserIncome;
import pojo_k.HomeUserPayout;
import util_k.SqlSessionFactoryUtils;

public class HomeUserService {
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();

    //用户登录
    public HomeUser homeUserSelect(String homeuserName,String homeuserPassword){
        //2.获取SqlSession
        SqlSession sqlSession = factory.openSession();

        //3.获取HomeUserMapper
        HomeUserMapper mapper = sqlSession.getMapper(HomeUserMapper.class);

        //4.调用方法
        HomeUser homeUser = mapper.select(homeuserName,homeuserPassword);

        //释放资源
        sqlSession.close();

        //返回查询到的数据
        return homeUser;
    }

    //根据用户名查找
    public HomeUser homeUserSelectByName(String homeuserName){
        //2.获取SqlSession
        SqlSession sqlSession = factory.openSession();

        //3.获取HomeUserMapper
        HomeUserMapper mapper = sqlSession.getMapper(HomeUserMapper.class);

        //调用方法
        HomeUser homeUser = mapper.selectByName(homeuserName);

        //释放资源
        sqlSession.close();

        //返回查询到的数据
        return homeUser;

    }

    //用户注册
    public void homeUserAdd(HomeUser homeUser){
        //2.获取SqlSession
        SqlSession sqlSession = factory.openSession();

        //3.获取HomeUserMapper
        HomeUserMapper mapper = sqlSession.getMapper(HomeUserMapper.class);

        //调用方法
        mapper.add(homeUser);

        //提交事务
        sqlSession.commit();
        //释放资源
        sqlSession.close();
    }

    //用户收入记账
    public void homeUserIncomeAdd(HomeUserIncome homeUserIncome){
        //2.获取SqlSession
        SqlSession sqlSession = factory.openSession();

        //3.获取HomeUserMapper
        HomeUserMapper mapper = sqlSession.getMapper(HomeUserMapper.class);

        //存入
        mapper.addIncome(homeUserIncome);

        //提交事务
        sqlSession.commit();
        //释放资源
        sqlSession.close();
    }

    //用户取款记账
    public void homeUserPayoutAdd(HomeUserPayout homeUserPayout){
        //2.获取SqlSession
        SqlSession sqlSession = factory.openSession();

        //3.获取HomeUserMapper
        HomeUserMapper mapper = sqlSession.getMapper(HomeUserMapper.class);

        //存入
        mapper.addPayout(homeUserPayout);

        //提交事务
        sqlSession.commit();
        //释放资源
        sqlSession.close();
    }





}
